// 引入异步请求
import request from '../../utils/request.js'
Page({
  getUserInfo(e){
    const { encryptedData, rawData, iv, signature} = e.detail;
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          request({
            url: '/users/wxlogin',
            method : 'post',
            data: {
              encryptedData,
              rawData,
              iv,
              signature,
              code: res.code
            }
          }).then(res =>{
            // console.log(res)
            // 获取token值
            const {token} = res.data.message;
            //  将token保存到本地
            wx.setStorageSync('token', token)
            // 弹框提示
            wx.showToast({
              title: '授权成功',
              icon: 'success',
              duration: 2000
            })
             setTimeout(()=>{
               // 返回到支付页面
               wx.navigateBack({
                 delta: 1
               })
             },2000)
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }
 
})